<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/OutsourceProcessReport.jsx或OutsourceProcessReport.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :addAfter="addAfter" :delAfter="delAfter"
        :updateBefore="updateBefore" :rowClick="rowClick" :modelOpenBefore="modelOpenBefore"
        :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
        <template #gridFooter>
            <div class="processOutsourceSettlement">
                <processOutsourceSettlement ref="processOutsourceSettlementRef"></processOutsourceSettlement>
            </div>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import extend from "@/extension/mes/mes/OutsourceProcessReport.jsx";
import viewOptions from './OutsourceProcessReport/options.js'
import processOutsourceSettlement from './ProcessOutsourceSettlement.vue'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
import { useRoute } from 'vue-router'

const processOutsourceSettlementRef = ref();
const route = useRoute()
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    if (route.name == "OutsourceNotice") {
        gridRef.load = false
    }
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })

    if (route.name == "OutsourceNotice") {
        gridRef.height = 200;
    }

    // gridRef.buttons.splice(3, 0, {
    //     name: '生产入库', //按钮名称
    //     icon: 'el-icon-document', //按钮图标:组件示例->图标
    //     //primary、success、warning、error、info、text、danger
    //     type: 'primary',
    //     plain: true,
    //     onClick: () => {

    //     }
    // })
    gridRef.buttons.splice(4, 0, {
        name: '委外结算', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'primary',
        plain: true,
        onClick: () => {
            processOutsourceSettlementAddOpen()
        }
    })
    gridRef.buttons.forEach(button => {
        // button.hidden = true
        if (button.name == "查询") {
            button.hidden = false
        }
        if (button.name == "删除") {
            button.name = "撤销报工"
            button.hidden = false
        }
        if (button.name == "委外结算") {
            button.hidden = false
        }
    });

}
const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    if (route.name == "OutsourceNotice") {
        const noticeRows = getNoticeSelectRow();
        if (!noticeRows.length) {
            proxy.$message.error('请先选择【委外通知】数据')
            return false;
        }
        param.wheres.push({ name: "OutsourceNoticeId", value: noticeRows[0].OutsourceNoticeId })
    }

    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const addAfter = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    const noticeRows = getNoticeSelectRow();
    noticeSearch(noticeRows[0]);
    // noticeToggleRowSelect(noticeRows)
    return true;
}
const delAfter = async (res, rows) => { //查询界面的表删除后方法
    const noticeRows = getNoticeSelectRow();
    noticeSearch(noticeRows[0]);
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const getNoticeSelectRow = () => {
    const fn = proxy.base.getItem('getNoticeSelectRow');
    return fn();
}
const noticeSearch = (row) => {
    const fn = proxy.base.getItem('noticeSearch');
    return fn(row);
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
    gridRef.buttons.forEach(button => {
        button.hidden = true
        if (button.name == "删除" || button.name == "撤销报工") {
            button.name = "撤销报工"
            button.hidden = false
        }
        if (button.name == "生产入库" && row.IsLasted == 1) {
            button.hidden = false
        }
        if (button.name == "委外结算") {
            button.hidden = false
        }
    });
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    //弹出框打开后方法,设置表单默认值,按钮操作等
}
const processOutsourceSettlementAddOpen = () => {

    var rows = gridRef.getSelectRows()
    if (!rows.length) {
        proxy.$message.error("请选择委外工序汇报！");
        return
    }
    if (rows[0].AuditStatus == 0 || rows[0].AuditStatus == null) {
        proxy.$message.error("委外工序汇报未审核！");
        return
    }
    if (rows[0].WaitReportQty <= rows[0].SettlmentQty) {
        proxy.$message.error("委外工序汇报已结算完成！");
        return
    }
    let url = 'api/ProcessOutsourceSettlement/BuildOutsourceSettlement';

    proxy.http.post(url, rows[0]).then((result) => {
        setTimeout(() => {

            processOutsourceSettlementRef.value.$refs.grid.editFormFields.OutsourceProcessReportId = result.outsourceProcessReportId
            processOutsourceSettlementRef.value.$refs.grid.editFormFields.DepartmentId = result.departmentId
            processOutsourceSettlementRef.value.$refs.grid.editFormFields.ProductTaskId = result.productTaskId
            processOutsourceSettlementRef.value.$refs.grid.editFormFields.BillDate = proxy.base.getDate()

            result.outsourceSettlementDetail.forEach(detailRow => {
                processOutsourceSettlementRef.value.$refs.grid.getTable("OutsourceSettlementDetail").addRow({
                    MaterialId: detailRow.materialId,
                    MaterialNo: detailRow.materialNo,
                    MaterialName: detailRow.materialName,
                    Spec: detailRow.spec,
                    UnitId: detailRow.unitId,
                    OutQty: detailRow.outQty,
                    SettlementQty: detailRow.settlementQty,
                })
            });

        }, 400);
        processOutsourceSettlementRef.value.$refs.grid.add()
    })
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
